package com.issoft.studentresitor.controller.student;

import com.issoft.studentresitor.domain.pojo.Notice;
import com.issoft.studentresitor.domain.pojo.Result;
import com.issoft.studentresitor.domain.vo.NoticeVo;
import com.issoft.studentresitor.service.INoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "学生公告内容接口")
@Slf4j
@RestController
@RequestMapping("/student/notice")
public class StuNoticeController {
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private RedisTemplate redisTemplate;
    @ApiOperation(value = "查询所有公告")
    @GetMapping("/queryAll")
    public Result queryAll() {
        //构建redis缓存key
        String key = "notice";
        //查询缓存
        List<Notice> list = (List<Notice>) redisTemplate.opsForValue().get(key);
        if (list != null && list.size() > 0) {
            //缓存存在
            log.info("缓存命中...");
            return Result.success(list);
        }
         list = noticeService.list();
        redisTemplate.opsForValue().set(key, list);
        log.info("报存到缓存：{}", list);
        return Result.success(list);
    }
    @ApiOperation(value = "模糊查询")
    @GetMapping("/query")
    public Result query(@RequestParam(value = "title", required = false) String title,
                        @RequestParam(value = "noticeType", required = false) String noticeType,
                        @RequestParam(value = "publishTime", required = false)LocalDateTime publishTime) {
        List<NoticeVo> noticeVo = noticeService.queryNotice(title, noticeType, publishTime);
        log.info("模糊查询的公告信息为：{}", noticeVo);
        return Result.success(noticeVo);
    }
}
